package com.bright.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
    public static String code(String str){
        try {
        MessageDigest md = null;
            md = MessageDigest.getInstance("MD5");
        md.update(str.getBytes());
        byte[] bytedigests = md.digest();
        int i ;
        StringBuffer sbuffer = new StringBuffer("");
        for (int j = 0; j < bytedigests.length; j++) {
            i = bytedigests[j];
            if (i<0)
                i += 256;
            if (i<16)
                sbuffer.append("0");
            sbuffer.append(Integer.toHexString(i));
        }
//        32位加密
        return sbuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }return "";
    }


    public static void main(String[] args) throws NoSuchAlgorithmException {
        System.out.println(MD5Utils.code("123456"));
    }
}
